// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

exports.main = async (event, context) => {
  const db = cloud.database()
  const openid = event.openid
  const orders = db.collection('orders')

  const unpaid = await orders.where({ _openid: openid, payment_status: 'unpaid' }).count()
  const unshipped = await orders.where({ _openid: openid, payment_status: 'paid', status: 'pending' }).count()
  const unreceived = await orders.where({ _openid: openid, payment_status: 'paid', status: 'shipped' }).count()
  const afterSale = await orders.where({ _openid: openid, status: 'afterSale' }).count()

  return {
    unpaid: unpaid.total,
    unshipped: unshipped.total,
    unreceived: unreceived.total,
    afterSale: afterSale.total
  }
} 